package flashcardtest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.*; // for ArrayList


public class User {
	private String name;
	private Map<String, int[]> records;
	int correct;
	int incorrect;
	String currentSet;
	
	public User(String name) {
		this.name = name;
		this.records = new HashMap<String, int[]>();
		this.correct = 0;
		this.incorrect = 0;
		this.currentSet = "";
	}
	public String DisplayResults() {
		String results = "Statistics for user: " + name + "\n";
		results += "--------------------------------\n";

		Iterator itr = records.entrySet().iterator();
		while(itr.hasNext()) {
			Map.Entry pairs = (Map.Entry)itr.next();
			String tempSet = (String)pairs.getKey();
			int[] data = (int[])pairs.getValue();
			float correctRate = 0;
			if (data[0] > 0 && data[1] == 0) {
				correctRate = 1;
			}
			else if (data[0] + data[1] > 0) {
				correctRate = (float)data[0] / ((float)data[0] + (float)data[1]);				
			}
			String correctRateString = String.format("%.3g", (100*correctRate));
			results += tempSet + ":\n";
			results += "Correct Answers: " + Integer.toString(data[0]) + "\n";
			results += "Incorrect Answers: " + Integer.toString(data[1]) + "\n";
			results += "Percentage Correct: " + correctRateString + "%\n";
			results += "--------------------------------\n";
		}

		return results;
	}
	public void addRecord(String newSet, int corr, int incorr) {
		int[] intarray = {corr, incorr};
		records.put(newSet, intarray);
	}
	public void switchSet(String newSet) {
		currentSet = new String(newSet);
		int[] data = records.get(newSet);
		if (data != null) { // data was already registered for this set
			correct = data[0];
			incorrect = data[1];
		}
		else { // there was no record for this set. Create one.
			correct = 0;
			incorrect = 0;
			int[] newData = {0,0};
			records.put(newSet, newData);
		}
		//System.out.println("Switched set. Set: " + currentSet + " c: " + Integer.toString(correct) + "  i: " + Integer.toString(incorrect)); 
	}
	public void correct() {
		correct++;
		addRecord(currentSet, correct, incorrect);
	}
	public void incorrect() {
		incorrect++;
		addRecord(currentSet, correct, incorrect);
	}
	public String getName() {return name;}
	public String getFileName() {return name + "Data.txt";}
	public int getCorrect() {
		return correct;
	}
	public int getIncorrect() {
		return incorrect;
	}
	public String getCurrentSet() {
		return currentSet;
	}
	public void save(BufferedWriter out) {
		
		try {
			Iterator itr = records.entrySet().iterator();
			while(itr.hasNext()) {
				Map.Entry pairs = (Map.Entry)itr.next();
				int[] data = (int[])pairs.getValue();
				
				out.write(pairs.getKey() + "\n");
				out.write(Integer.toString(data[0]) + "\n");
				out.write(Integer.toString(data[1]) + "\n");
			}
		}
		catch (IOException e) {
			
		}
	}

	public void load(BufferedReader reader) {
		//System.out.println("Loading User " + name);
		String temp = "";
		int temp_correct = 0;
		int temp_incorrect = 0;
		
		try{
			while ((temp = reader.readLine()) != null) {
				// read one entry
				if (temp != null) {
					String setName = temp;
					temp = reader.readLine();
					if (temp != null) {
						temp_correct = Integer.parseInt(temp);
						
						temp = reader.readLine();
						if (temp != null) {
							temp_incorrect = Integer.parseInt(temp);
										
							this.addRecord(setName, temp_correct, temp_incorrect);
						}
					}
				}
			}
		}
		catch (IOException e) {
			
		}
	
	}
}
